package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//收货地址
@RestController
public class AddressController {

    @Autowired
    private AddressService addressService;

    //查询收货地址列表
    @GetMapping("/address/list")
    public ResultInfo findList(){
        List<Address> addressList = addressService.findList();
        return ResultInfo.success(addressList);

    }

    //新增收货地址
    @PostMapping("/address")
    public ResultInfo add(@RequestBody Address address){//接受传入的参数
        addressService.add(address);
        return ResultInfo.success();//返回结果
    }

    //设置默认收货地址
    @PutMapping("/address/default")
    public ResultInfo setDefault(@RequestBody Map<String,Long> param){//接受传入的参数
        Long id = param.get("id");//取出传入id
        addressService.setDefault(id);
        return ResultInfo.success();//返回结果
    }

    //查看默认收货地址
    @GetMapping("/address/default")
    public ResultInfo findDefault(){
        Address address = addressService.findDefault();//调用
        return ResultInfo.success(address);//返回结果
    }

    //修改地址回显
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        Address address = addressService.findById(id);
        return ResultInfo.success(address);
    }

    //修改收货地址
    @PutMapping("/address")
    public ResultInfo updateAddress(@RequestBody Address address){
        addressService.updateAddress(address);
        return ResultInfo.success();
    }
    //删除地址
    @DeleteMapping("/address")
    public ResultInfo delete(Long ids ){
        addressService.delete(ids);
        return ResultInfo.success();
    }
}
